import { graphHelper } from 'src/api/GraphHelper';

export class GraphTabUtil {
  static addRecentDiagram(id:string){
    !app.ui.recentDiagramIds.includes(id) && app.ui.recentDiagramIds.push(id);
  }
  static openDiagram(diagramId:string){
    if (app.ui.activeTabId === diagramId) return;
    const exist = app.graphTabs.find((tab) => tab.id === diagramId);
    if (exist) {
      this.changeTab(diagramId);
    } else {
      this.addDiagram(diagramId);
    }
  
  }
  /**
   * 新增
   * @returns 
   */
  static getNewTabIndex(){
    if (!app.ui.activeTabId) return app.graphTabs.length;
    let index = app.graphTabs.findIndex((tab) => tab.id === app.ui.activeTabId);
    return index + 1;
  
  }
  
  static addDiagram(diagramId:string) {
  
    //// const allDiagram = graphHelper.getProjectAllDiagrams(app.activeProjectId);
    //// console.log(allDiagram);
    //// if (!allDiagram) return;
    const diagramNode =  graphHelper.getGraphNodeById(diagramId, app.activeProjectId);
    if (!diagramNode) return;
    const typeName = diagramNode.getTypeName();
    const typeConfigItem = app.activeProject.configManager.getTypeNameConfigItem(typeName);
  
    const tab = {
      id: diagramNode.id,
      diagramType: typeName,
      type: typeConfigItem.type,
      graphNode: diagramNode,
      textSizeCache: {},
      graph: undefined,
      inModal: false,
      visible: true,
      modalVisible: false
    };
    app.ui.graphTabs.forEach(i => {
      if (i.inModal) return;
      i.visible = false;
    });
    //// TODO
    app.ui.graphTabs.splice(this.getNewTabIndex(), 0, tab);
    app.ui.activeTabId = tab.id;
    app.ui.currentTabId = tab.id;
  
    this.addRecentDiagram(diagramId); //// 添加最近打开记录
  }
  /**
   * 变更当前激活的tabId
   * @param tabId 
   */
  static changeTab(tabId:string) {
    app.ui.activeTabId = tabId;
  
    const tab = app.graphTabs.find(i => i.id === tabId);
    tab.visible = true;  
    if (!tab.inModal){
      app.ui.currentTabId = tabId;
    }

  }
  /**
     * 模型上删除了画布时，发射diagram-remove事件，触发此方法，关闭画布同时移除最近打开画布
     */
  static handleRemoveDiagram({ id, ids }:{id:string, ids:string[]}) {
    if (id) {
      this.removeDiagram(id);
      const index = app.ui.recentDiagramIds.indexOf(id);
      if (index > -1){
        app.ui.recentDiagramIds.splice(index, 1);
      }
    } else if (ids) {
      ids.forEach(i => {
        this.removeDiagram(i);
        const index = app.ui.recentDiagramIds.indexOf(id);
        if (index > -1){
          app.ui.recentDiagramIds.splice(index, 1);
        }
      });
    }
  
  }
  /**
     * 点击tab关闭按钮时的移除画布
     */
  static removeDiagram(diagramId:string) {
    console.log('remove-----');
  
    let index = app.graphTabs.findIndex((tab) => tab.id === diagramId);
    //// const tab = app.graphTabs[index];

    if (index > -1) {
      app.graphTabs.splice(index, 1);
      const newIndexTab =  app.graphTabs[index];
      if (newIndexTab){
        app.ui.activeTabId = newIndexTab.id;
      } else if (app.graphTabs.length) {
        app.ui.activeTabId = app.graphTabs[app.graphTabs.length - 1].id;
      } else {
        app.ui.activeTabId = '';
      }
      if (app.activeGraphTab){
        if (!app.activeGraphTab.inModal) {
          app.ui.currentTabId = app.activeGraphTab.id;

        } else {
          const inLineTabs = app.graphTabs.filter(i => !i.inModal);
          if (inLineTabs.length){
            app.ui.currentTabId = inLineTabs[inLineTabs.length - 1].id;
          } else {
            app.ui.currentTabId = '';
          }
        }
        
      } else {
        app.ui.currentTabId = '';
      }
    
      //// app.graphTabs.splice(index, 1);
      //// if (app.ui.activeTabId === diagramId) {
      ////   const preTab = app.graphTabs[index - 1];
      ////   const firstTab = app.graphTabs[0];
      ////   app.ui.activeTabId = (preTab || firstTab || {}).id;
      //// }

      //// if (app.ui.currentTabId === diagramId) {

      //// }
    }
  
  }
  static clearDiagram() {
    app.ui.graphTabs = [];
    app.ui.activeTabId = undefined;
  }

}